 <?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-04-15 08:58:26
 * @version 1.0
 */

class MemberAction extends AdminAction {
    
    public function index(){
    	$key  = I('get.key');
    	$userType  = I('get.userType');
		$wxname  = I('get.wxname');
		$obj = M('Member');		
		if ($key != '') {
			$map['id'] = $key;
		}

		if ($userType != '') {
			$map['userType'] = $userType;
		}

		if ($wxname != '') {
			$map['wxname'] = array('like',$wxname);
		}

		$count = $obj->where($map)->count();
		import("@.ORG.Page");
		$page = new Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('createTime desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach ($list as $key => $value) {
			if ($father = $obj->where('wxid="'.$value['fid'].'"')->getField('wxname')) {
				$list[$key]['father'] = $father;
			}else{
				$list[$key]['father'] = '无';
			}
			unset($map);
			$map['fid'] = $value['wxid'];
			$map['userGroup'] = 1;
			$list[$key]['buyUser'] = M('Member')->where($map)->count();
		}
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->display();
    }

	#编辑
	public function edit(){
		$wxid = $_GET['wxid'];
		if (!isset ($wxid)) {
			$this->error('参数错误');
		}
		$obj = M('Member');
		$map['wxid'] = $wxid;
		$list = $obj->where($map)->find();

		if (!$list) {
			$this->error('信息不存在');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}
	
	#更新
	public function update() {
		$obj = D('Member');
        if ($data = $obj->create()) {
            if ($list = $obj->save($data)) {                
                $this->success('操作成功');
            } else {
                $this->success('操作失败');
            }            
        } else {
            $this->success($obj->getError());
        }
	}

	#删除
	public function del(){

		$id=I('post.selectedids');

		if($id==''){
			$state = "您没有选择任何信息！";
		}else{
			foreach ($id as $v) {
				$obj = M('Member');
				$where['id'] = $v;					
				$obj->where($where)->delete();
			}
			$state = 'SUCCESS';
	        $message = '删除成功';
	        $url = U('Member/index');
		}
		echo $this->echo_json_str($state,$message,$url);
	}
}